package com.itany.corejava.day17;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TestClientC {

    public static void main(String[] args) {
        Socket s=null;
        Scanner sc=new Scanner(System.in);

        PrintWriter pw=null;

        BufferedReader br=null;

        try{

            s=new Socket("127.0.0.1",8888);

            System.out.println("name:");
            String name=sc.nextLine();

            pw=new PrintWriter(s.getOutputStream());

            pw.println(name);

            pw.flush();

            br=new BufferedReader(
                    new InputStreamReader(
                            s.getInputStream()
                    )
            );

            String message=br.readLine();

            System.out.println(message);

        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(pw!=null){
                pw.close();
            }
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
